#ifndef TINY_MUDUO_ACCEPTOR_H_
#define TINY_MUDUO_ACCEPTOR_H_

#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>

#include <memory>
#include<functional>

#include "noncopyable.h"
//设置文件描述符属性的功能放在函数里调用
namespace tiny_muduo{
	class EventLoop;
	class Address;
	class Channel;
	
	class Acceptor:public NonCopyAble{
		public:
			typedef std::function<void(int)> NewConnectionCallback;
	
			Acceptor(EventLoop* loop,const Address& address);
			~Acceptor();

			void BindListenFd(const Address& address);
			void Listen();
			void NewConnection();

			int SetSockoptKeepAlive(int fd){
				int option_val=1;
				return setsockopt(fd,SOL_SOCKET,SO_KEEPALIVE,
								  &option_val,static_cast<socklen_t>(sizeof(option_val)));
/*
原函数长这样int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
分别是sockfd:文件描述符
	  level:选项的级别。对于大多数套接字选项，level可以是SOL_SOCKET（套接字级别的选项）或者特定的协议级别（如IPPROTO_TCP用于TCP套接字）。
	  optname:要设置的选项的名称
	  optval:指向选项值的指针
	  optlen:选项值的大小
*/	  
			}

			int SetSockoptReuseAddr(int fd){
				int option_val=1;
				return setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,
								  &option_val,static_cast<socklen_t>(sizeof(option_val)));
			}

			int SetSockoptTcpNoDelay(int fd){
				int option_val=1;
				return setsockopt(fd,IPPROTO_TCP,SO_KEEPALIVE,
								  &option_val,static_cast<socklen_t>(sizeof(option_val)));
			}

			void SetNewConnectionCalback(NewConnectionCallback&& callback){
				new_connection_callback_=std::move(callback);
			}

			void SetNewConnectionCallback(const NewConnectionCallback& callback){
				new_connection_callback_=callback;
			}
			
		private:
			EventLoop*loop_;
			int listenfd_;
			int idlefd_;
			std::unique_ptr<Channel> channel_;
			
			NewConnectionCallback new_connection_callback_;		
	};
}
#endif